package com.crowdfunding.controller;

import com.crowdfunding.bean.Member;
import com.crowdfunding.service.MemberService;
import com.crowdfunding.service.TicketService;
import com.crowdfunding.util.Page;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/authcert")
public class authCertController extends BaseController{
    @Autowired
    private TaskService taskService;
    @Autowired
    private RepositoryService repositoryService ;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private MemberService memberService;
    @RequestMapping("/index")
    public String index(){
        return "authcert/index";
    }
    @RequestMapping("/show")
    public String show(Integer memberid,Map<String,Object>map){
        Member member=memberService.getMemberById(memberid);
        List<Map<String,Object>>list=memberService.queryCertByMemberId(memberid);
        map.put("member",member);
        map.put("certimgs",list);
        return "authcert/show";
    }
    @ResponseBody
    @RequestMapping("/pass")
    public Object pass(Integer memberid,String taskid){
        start();
        try{
            taskService.setVariable(taskid,"flag",true);
            taskService.setVariable(taskid,"memberid",memberid);
            taskService.complete(taskid);
            success(true);
        }catch (Exception e){
            e.printStackTrace();
            success(false);
        }

        return end();
    }
    @ResponseBody
    @RequestMapping("/refuse")
    public Object refuse(Integer memberid,String taskid){
        start();
        try{
            taskService.setVariable(taskid,"flag",false);
            taskService.setVariable(taskid,"memberid",memberid);
            taskService.complete(taskid);
            success(true);
        }catch (Exception e){
            e.printStackTrace();
            success(false);
        }

        return end();
    }
    @RequestMapping("/pageQuery")
    @ResponseBody
    public Object pageQuery(@RequestParam(value = "pagesize",required = false,defaultValue = "5") Integer pagesize, @RequestParam(value = "pageno",required = false,defaultValue = "1") Integer pageno){
        start();

        try{
            Page page=new Page(pageno,pagesize);
            //1、查询后台backuse委托组的任务
            TaskQuery taskQuery = taskService.createTaskQuery().processDefinitionKey("auth")
                    .taskCandidateGroup("backuser");
            List<Task> tasks =taskQuery.listPage(page.getStartIndex(), pagesize);

            //2、根据任务查询流程定义（流程定义名称，流程定义版本）
            List<Map<String,Object>>data=new ArrayList<>();
            for (Task task :
                    tasks) {
                String processDefinitionId = task.getProcessDefinitionId();
                //流程定义
                ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
                Map<String,Object>map=new HashMap<>();
                map.put("taskid",task.getId());
                map.put("taskname",task.getName());
                map.put("procDefName",processDefinition.getName());
                map.put("proDefVersion",processDefinition.getVersion());
                //3、根据任务查询流程实例（根据流程实例的id查询流程单，查询用户信息）
                Member member=ticketService.getByMemberByPiid(task.getProcessInstanceId());
                map.put("member",member);
                data.add(map);

            }
            page.setData(data);
            Long count = taskQuery.count();
            page.setTotalsize(count.intValue());
            page(page);
            success(true);
        }catch (Exception e){
            success(false);
            message("流程任务查询出错！");
            e.printStackTrace();
        }

        return end();
    }
}
